
package com.mp.chart.markerview;

import android.content.Context;
import android.text.Html;
import android.view.Gravity;
import android.widget.TextView;


import com.mp.chart.data.CandleEntry;
import com.mp.chart.data.Entry;
import com.mp.chart.data.KLineData;
import com.mp.chart.formatter.DataFormatter;
import com.mp.chart.utils.HqKlineDb;
import com.mp.chart.utils.IndexModel;
import com.android.R;

public class MAMarkerView extends ValueMarkerView {

    private final String ma5Color;
    private final String ma10Color;
    private final String ma30Color;
    private TextView tvContent;
    private HqKlineDb hqKlineDb;

    public MAMarkerView(Context context, int layoutResource) {
        super(context, layoutResource);
        tvContent = (TextView) findViewById(R.id.tvContent);
        hqKlineDb = new HqKlineDb(context);
        ma5Color = DataFormatter.colorToString(getResources().getColor(R.color.ma5));
        ma10Color = DataFormatter.colorToString(getResources().getColor(R.color.ma10));
        ma30Color = DataFormatter.colorToString(getResources().getColor(R.color.ma30));
//        tvContent.setBackgroundColor(0xcc272c3f);
    }

    @Override
    public void refreshContent(Entry e, int dataSetIndex, int pos) {

//        if (pos == 1) {
//            tvContent.setGravity(Gravity.LEFT);
//        } else {
        tvContent.setGravity(Gravity.RIGHT);
//        }

        String topIndex = hqKlineDb.getTopIndex();
        CandleEntry data = (CandleEntry) e;

        KLineData kLineData = (KLineData) data.getData();
        if (topIndex.equals(IndexModel.MAJUST)) {
            float[] ma = {kLineData.ma5, kLineData.ma10, kLineData.ma30};
            String[] maString = new String[3];

            for (int i = 0; i < maString.length; i++) {
                if (!Float.isNaN(ma[i])) {
                    maString[i] = DataFormatter.getFormat(ma[i], "#0.00");
                } else {
                    maString[i] = "--";
                }
            }

            tvContent.setText(Html.fromHtml(String.format("<font color='%s'>MA%s:%s</font>&nbsp;&nbsp;&nbsp;" +
                            "<font color='%s'>MA%s:%s</font>&nbsp;&nbsp;&nbsp;" +
                            "<font color='%s'>MA%s:%s</font>"
                    , ma5Color, hqKlineDb.getMA1(), maString[0],
                    ma10Color, hqKlineDb.getMA2(), maString[1],
                    ma30Color, hqKlineDb.getMA3(), maString[2])));


        } else {
            float[] ma = {kLineData.maUp, kLineData.maMid, kLineData.maLow};
            String[] maString = new String[3];

            for (int i = 0; i < maString.length; i++) {
                if (!Float.isNaN(ma[i])) {
                    maString[i] = DataFormatter.getFormat(ma[i], "#0.00");
                } else {
                    maString[i] = "--";
                }
            }

            tvContent.setText(Html.fromHtml(String.format("<font color='%s'>%s:%s</font>&nbsp;&nbsp;&nbsp;" +
                            "<font color='%s'>%s:%s</font>&nbsp;&nbsp;&nbsp;" +
                            "<font color='%s'>%s:%s</font>"
                    , ma10Color,"MID", maString[1],
                    ma5Color, "UPPER", maString[0],
                    ma30Color, "LOWER", maString[2])));


        }
    }


}
